import hashlib
import time
import tkinter as tk
import tkinter.messagebox

from class_1_sqlite import DataBase
from class_2_sign_up import Page_sign_up
from class_4_end import Page_end


class Page_sign_in:  # 登录页面
    def __init__(self):
        self.window = tk.Tk()
        self.window.title("main")
        self.window.geometry("500x300")
        self.window.config(bg="#F9C03D")
        self.labelframe_1()
        self.labelframe_2()
        self.db = DataBase()
        self.window.mainloop()

    def labelframe_1(self):
        lf = tk.LabelFrame(
            self.window, text="Login", font=("微软雅黑", 10, "bold"), padx=10, pady=10
        )
        lf.place(x=20, y=20)

        label_username = tk.Label(lf, text="用户:", font=("微软雅黑", 10, "bold"), height=2)
        label_username.grid(row=1, column=1)

        entry_username = tk.Entry(lf, width=30, font=("微软雅黑", 10))
        entry_username.grid(row=1, column=2)

        label_password = tk.Label(lf, text="密码:", font=("微软雅黑", 10, "bold"), height=2)
        label_password.grid(row=2, column=1)

        entry_password = tk.Entry(lf, width=30, font=("微软雅黑", 10), show="*")
        entry_password.grid(row=2, column=2)

        button = tk.Button(lf, text="登录", width=40, height=1, command=self.main)
        button.grid(row=3, column=1, columnspan=2)

        self.entry_username = entry_username
        self.entry_password = entry_password

    def labelframe_2(self):
        lf = tk.LabelFrame(
            self.window, text="", font=("微软雅黑", 10, "bold"), padx=10, pady=10
        )
        lf.place(x=20, y=200)

        b2 = tk.Button(lf, text="注册", width=10, height=1, command=self.sign_up)
        b2.grid(row=1, column=1)

        b3 = tk.Button(lf, text="重置密码", width=10, height=1)
        b3.grid(row=1, column=3)

    def password_is_right(self):
        username = self.entry_username.get()
        password = self.entry_password.get()
        user_info = self.db.select(username)

        password_md5 = hashlib.md5("add_salt".encode(encoding="utf-8"))  # 加盐增加安全性
        password_md5.update(password.encode(encoding="utf-8"))
        if password_md5.hexdigest() == user_info[2]:
            print("密码正确，登录成功")
            tkinter.messagebox.showinfo(title="Tips", message="密码正确，登录成功")
            self.window.destroy()
            Page_end()
        else:
            password_false_count = user_info[6]
            if password_false_count == 0:
                self.db.update_password_false_count(username, 1)
                print("从来没错过,还可再试2次")
                tkinter.messagebox.showinfo(title="Tips", message="从来没错过,还可再试2次")
            elif password_false_count == 1:
                self.db.update_password_false_count(username, 2)
                print("还可再试1次")
                tkinter.messagebox.showinfo(title="Tips", message="还可再试1次")
            elif password_false_count == 2:
                self.db.update_password_false_count(username, 3)
                self.db.update_lock(username)
                print("错3次，已锁定")
                tkinter.messagebox.showinfo(title="Tips", message="错3次，已锁定")
            else:
                print("不应该有这种情况")
                tkinter.messagebox.showinfo(title="Tips", message="不应该有这种情况，代码逻辑有误")

    def main(self):
        username = self.entry_username.get()
        user_info = self.db.select(username)
        self.db.creat_if()

        if self.db.select(username) == None:  # 用户是否存在
            print("用户未注册，请先注册")
            tkinter.messagebox.showinfo(title="Tips", message="用户未注册，请先注册")
        else:
            if user_info[4] == "FALSE":  # 用户是否锁定
                self.password_is_right()
            else:
                locktime_db = time.mktime(
                    time.strptime(user_info[5], "%Y-%m-%d %H:%M:%S")
                )
                if time.time() - locktime_db > 60:
                    print("虽已锁定，但已超出60s，可重试")
                    self.db.update_unlock(username)
                    self.password_is_right()
                else:
                    print("已锁定，60s后再试")
                    tkinter.messagebox.showinfo(title="Tips", message="已锁定，N MIN后再试")

    def sign_up(self):
        self.window.destroy()
        Page_sign_up()


if __name__ == "__main__":
    Page_sign_in()
